package com.dynamicthreadpool.spring;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 动态线程池自动配置类
 * 参考XXL-JOB的自动配置方式
 */
@Configuration
@EnableConfigurationProperties(DynamicThreadPoolProperties.class)
public class DynamicThreadPoolAutoConfiguration {

    /**
     * 配置DynamicThreadPoolExecutor实例
     * @param properties 配置属性
     * @return DynamicThreadPoolExecutor实例
     */
    @Bean
    @ConditionalOnMissingBean
    public DynamicThreadPoolExecutor dynamicThreadPoolExecutor(DynamicThreadPoolProperties properties) {
        DynamicThreadPoolExecutor executor = new DynamicThreadPoolExecutor();
        
        // 设置配置参数
        executor.setPort(properties.getPort());
        executor.setAdminAddresses(properties.getAdminAddresses());
        executor.setAccessToken(properties.getAccessToken());
        executor.setAppname(properties.getAppname());
        
        return executor;
    }
}